package com.sx.pipeline.custom.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.text.TextUtils
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.lxj.xpopup.core.BubbleAttachPopupView
import com.sx.pipeline.R
import com.sx.pipeline.repos.http.res.CommonApplyListVO

/**
 * @Author: YY
 * @Date: 2022/5/23
 * @Description: TODO
 */
@SuppressLint("ViewConstructor")
class CustomBubbleAttachPopup(context: Context, private val applyList: List<CommonApplyListVO>, val listener: ItemClickListener)
    : BubbleAttachPopupView(context) {

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_bubble_attach_popup
    }

    override fun onCreate() {
        super.onCreate()
        val recyclerView = findViewById<RecyclerView>(R.id.recycler_icon)
        recyclerView.layoutManager = GridLayoutManager(context, 4)
        val homeIconAdapter = IconAdapter(applyList as MutableList<CommonApplyListVO>)
        homeIconAdapter.setOnItemClickListener { adapter, view, position ->
            val data = adapter.data[position] as CommonApplyListVO
            listener.onClickItem(position, data)
            dismiss()
        }
        recyclerView.adapter = homeIconAdapter
    }

    interface ItemClickListener {
        fun onClickItem(position: Int, itemData: CommonApplyListVO)
    }

    class IconAdapter(dataList: MutableList<CommonApplyListVO>): BaseQuickAdapter<CommonApplyListVO, BaseViewHolder>(
        R.layout.item_home_icon, dataList) {

        override fun convert(holder: BaseViewHolder, item: CommonApplyListVO) {
            if (!TextUtils.isEmpty(item.iconUrl)) {
                Glide.with(context)
                    .load(item.iconUrl)
                    .error(R.drawable.ic_mine_release)
                    .into(holder.getView(R.id.image_icon))
            }
            if (!TextUtils.isEmpty(item.name)) {
                holder.setText(R.id.tv_name, item.name)
            }
        }
    }
}